/*var mongo = require('mongodb').MongoClient, 
	client = require('socket.io').listen(8080).sockets;

mongo.connect('mongodb://127.0.0.1/chat',function(err,db){
	if(err)throw err;
		client.on('connection',function(socket){
			
			var col= db.collection('messages');
			
			
			//입력대기
			
			//이름이나 내용이 입력되지 않으면 db에 입력되지 않는다.
			socket.on('input',function(data){
				var name= data.name,
					message=data.message,
					whitespacePettern=/^|*$/;
				if(whitespacePattern.test(name)|| whitespacePattern,test(message)){
					console.log('Invalid.');
				}else{
					col.insert({name: name, message: message}, function(){
						console.log('inserted');
					});
				}
				
			});
		});	
});


client.on('connection',function(socket){
	console.log('Some');
});*/

var socketio = require('socket.io')
, express = require('express')
, http = require('http')
, fs = require('fs');

var app = express();
app.set('port', 3033);
var server = http.createServer(app).listen(app.get('port'), function () {
    console.log("express server listening on port " + app.get('port'));
});

// 라우팅 설정
app.get('/', function (req, res) {
    fs.readFile('index.html', function (error, data) {
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(data);
    });
});

var io = socketio.listen(server);

// Socket Connection
io.sockets.on('connection', function (socket) {

    

    // Message 전송 받은 것을 다른 Client 에게 보내기.
    socket.on('message', function (message) {               
        io.sockets.emit('message',message);

    });
});
